<?php
class OEmbed {

    private static $oembedUrls=array(
            'www.youtube.com' => 'http://www.youtube.com/oembed?url=$1&format=json',
            'www.youtu.be' => 'http://www.youtube.com/oembed?url=$1&format=json',
            'www.vimeo.com' => 'http://vimeo.com/api/oembed.xml?url=$1&format=json',
            'www.flickr.com' => 'http://www.flickr.com/services/oembed/?url=$1&format=json',
            'www.flickr.kr' => 'http://www.flickr.com/services/oembed/?url=$1&format=json',
            'www.hulu.com' => 'http://www.hulu.com/api/oembed.json?url=$1',
            'www.viddler.com' => 'http://www.viddler.com/oembed/?url=$1&format=json',
            //'www.viddler.com' => 'http://lab.viddler.com/services/oembed/?url=$1&format=json',
            'www.qik.com' => 'http://qik.com/api/oembed.json?url=$1',
            'www.revision3.com' => 'http://revision3.com/api/oembed/?url=$1&format=json',
            'www.collegehumor.com' => 'http://www.collegehumor.com/oembed.json?url=$1',
            'www.jest.com' => 'http://www.jest.com/oembed.json?url=$1',
            
            /*
            'www.dailymotion.com' => 'http://www.dailymotion.com/api/oembed?url=$1&format=json',
            'www.blip.tv' => 'http://blip.tv/oembed/?url=$1&format=json',
            'www.scribd.com' => 'http://www.scribd.com/services/oembed?url=$1&format=json',
            'www.wordpress.tv' => 'http://wordpress.tv/oembed/?url=$1&format=json',
            'www.5min.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            
            'www.thedailyshow.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            'www.funnyordie.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            'www.livejournal.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            'www.metacafe.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            'www.xkcd.com' => 'http://www.oohembed.com/oohembed/?url=$1',
            'www.yfrog.com' => 'http://www.oohembed.com/oohembed/?url=$1'
             */
        );
    
    // available type: photo, video, link, rich
    public static function getResponse($url, $type, $options=array()){
        //$parts=parse_url($url);
        //$host=$parts['host'];
        $host='www.'.self::getDomain($url);
        if(empty($host) || !array_key_exists($host, self::$oembedUrls)){
            return array();
        }else{
            $width=isset($options['width']) && is_numeric($options['width'])? trim($options['width']):'';
            $height=isset($options['height']) && is_numeric($options['height'])? trim($options['height']):'';
            
            $url=str_replace('$1', urlencode($url), implode('&', array_filter(array(
                    self::$oembedUrls[$host],
                    empty($width)? '': 'maxwidth='.$width,
                    empty($height)? '':'maxheight='.$height,
                ))));
            
            if (1) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                $content=curl_exec($ch);
                curl_close($ch);
            } else {
                $content = @file_get_contents($url);
            }
            
            $oembedData=json_decode($content);
            
            return !empty($oembedData) && $oembedData->type==strtolower($type)? $oembedData: array();
        }
    }
    
    public static function getDomain($url){
        $url = trim($url);
        $url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url); 
        $url = preg_replace("/\/.*$/is" , "" ,$url); 
        return strtolower($url);
    }
    
}